package cn.regexp.web.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.regexp.domain.ShopGoodsBook;
import cn.regexp.domain.ShopUser;
import cn.regexp.redis.service.IShopUserRedisService;
import cn.regexp.service.IGoodsBookService;
import cn.regexp.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("goodsBooks")
public class GoodsBookController {
    @Autowired
    private IGoodsBookService goodsBookService;
    @Autowired
    private IShopUserRedisService shopUserRedisService;

    @PostMapping("/bookGoods")
    public JsonResult<String> bookGoods(@RequestBody ShopGoodsBook shopGoodsBook, HttpServletRequest request) {
        if (ObjectUtil.isEmpty(shopGoodsBook)){
            return JsonResult.defaultError();
        }
        String token = request.getHeader("token");
        ShopUser shopUser = shopUserRedisService.getUserByToken(token);
        if (ObjectUtil.isEmpty(shopUser)) {
            return JsonResult.noLogin();
        }
        Boolean res = goodsBookService.bookGoods(shopGoodsBook, shopUser);
        return res ? JsonResult.success() : JsonResult.defaultError();
    }

}
